package com.gf.research.cockpit.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 应用健康指标工具类
 *
 * @Author: wangjunchao(王俊超)
 * @Time: 2018-08-07 17:07
 **/
public class HealthUtils {

    /**
     * HTTP请求超时时间
     */
    private final static int TIME_OUT = 10 * 1000;
    private final static int OK       = 200;

    private static final Logger logger = LoggerFactory.getLogger(HealthUtils.class);

    public static class Status {
        private String  name;
        private boolean health;
        private String  cause;

        public Status() {
        }

        public Status(String name, boolean status, String cause) {
            this.name = name;
            this.health = status;
            this.cause = cause;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isHealth() {
            return health;
        }

        public void setHealth(boolean health) {
            this.health = health;
        }

        public String getCause() {
            return cause;
        }

        public void setCause(String cause) {
            this.cause = cause;
        }
    }

    /**
     * 数据库连接测试，当需要测试数据库连接时，需要实现此接口
     *
     * @author: wangjunchao(王俊超)
     * @date: 2018-10-29 14:46:34
     **/
    public interface DbConnection {
         int connect();
    }

    /**
     * 测试HTTP连接是否可用
     *
     * @param address 域名地址
     * @return true:连接正常，false：连接不正常
     */
    public static Status testHttpConnection(String name, String address) {
        Status status = new Status(name, true, null);
        try {
            URL url = new URL(address);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setUseCaches(false);
            // 设置超时时间
            connection.setConnectTimeout(TIME_OUT);
            if (OK != connection.getResponseCode()) {
                status.setHealth(false);
                status.setCause(connection.getResponseMessage());
            }
        } catch (Exception e) {
            status.setCause(e.getMessage());
            status.setHealth(false);
            logger.error(String.format("request error, the host is %s", address), e);
        }

        return status;
    }

    /**
     * 测试S3服务是否正常
     *
     * @param s3 S3服务
     * @return true:连接正常，false：连接不正常
     */
   /* public static Status testS3Connection(String name, AmazonS3 s3) {
        Status status = new Status(name, true, null);
        try {
            // 列出所有的桶，如果不出错就没有问题
            s3.listBuckets();

        } catch (Exception e) {
            status.setCause(e.getMessage());
            status.setHealth(false);
            logger.error("test amazon s3 connection error", e);
        }
        return status;
    }*/

    /**
     * 测试邮件服务是否正常
     *
     * @param mailSender 邮件发送器
     * @return true:连接正常，false：连接不正常
     */
    /*public static Status testMailConnection(String name, JavaMailSenderImpl mailSender) {
        Status status = new Status(name, true, null);
        try {
            mailSender.testConnection();
        } catch (Exception e) {
            status.setCause(e.getMessage());
            status.setHealth(false);
            logger.error("test mail connection error", e);
        }
        return status;
    }*/

        public static Status testDbConnection(String name, DbConnection connection) {
        Status status = new Status(name, true, null);
        try {
            connection.connect();
        } catch (Exception e) {
            status.setCause(e.getMessage());
            status.setHealth(false);
            logger.error(e.getMessage(), e);
        }

        return status;
    }

   /* public static Status testRedisConnection(String name, RedisTemplate template) {
        Status status = new Status(name, true, null);
        RedisConnection connection = null;
        try {
            connection = template.getConnectionFactory().getConnection();
            final String pong = "PONG";
            String result = connection.ping();
            if (!pong.equalsIgnoreCase(result)) {
                status.setHealth(false);
                status.setCause(result);
            }
        } catch (Exception e) {
            status.setCause(e.getMessage());
            status.setHealth(false);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }

        return status;
    }*/
}
